﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Serialization;

namespace _70C
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
            str += "<CSubmitState xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ";
            str += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://tempuri.org/\">";
            str += "<State>1032</State>";
            str += "<MsgID>0</MsgID>";
            str += "<MsgState>自由签名的产品101288809,签名格式不正确</MsgState>";
            str += "<Reserve>0</Reserve>";
            str += "</CSubmitState>";

            try
            {
                var v=GetXmlByKey(str, "State");
                var s = XmlSerialize.DeserializeXML<CSubmitState>(str);
                Console.WriteLine(s);  
            }
            catch (Exception)
            {
                
                throw;
            }
            
        }

        public string GetXmlByKey(string xml,string key)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            XmlNodeList xmlNodeList = doc.SelectSingleNode("").ChildNodes;

            foreach (XmlElement xl1 in xmlNodeList)
            {
                if (xl1.Name == key)
                {
                   return xl1.InnerText ;
                }
            }

            return "";
        }
    }

    public class CSubmitState
    {
        public int State { get; set; }
        public int MsgID { get; set; }
        public string MsgState { get; set; }
        public int Reserve { get; set; }
    }

    public class XmlSerialize
    {
        /// <summary>  
        /// 反序列化XML为类实例  
        /// </summary>  
        /// <typeparam name="T"></typeparam>  
        /// <param name="xmlObj"></param>  
        /// <returns></returns>  
        public static T DeserializeXML<T>(string xmlObj)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            using (StringReader reader = new StringReader(xmlObj))
            {
                return (T)serializer.Deserialize(reader);
            }
        }

        /// <summary>  
        /// 序列化类实例为XML  
        /// </summary>  
        /// <typeparam name="T"></typeparam>  
        /// <param name="obj"></param>  
        /// <returns></returns>  
        public static string SerializeXML<T>(T obj)
        {
            using (StringWriter writer = new StringWriter())
            {
                new XmlSerializer(obj.GetType()).Serialize((TextWriter)writer, obj);
                return writer.ToString();
            }
        }
    }  
}